package cn.tedu.tmall.admin.mall.service.impl;

import cn.tedu.tmall.admin.mall.dao.repository.ICategoryRepository;
import cn.tedu.tmall.admin.mall.dao.repository.IGoodsRepository;
import cn.tedu.tmall.admin.mall.pojo.param.MallGoodsAddParam;
import cn.tedu.tmall.admin.mall.pojo.po.CategoryPO;
import cn.tedu.tmall.admin.mall.pojo.po.GoodsDetailPO;
import cn.tedu.tmall.admin.mall.pojo.po.GoodsPO;
import cn.tedu.tmall.admin.mall.pojo.vo.GoodsItemListVO;
import cn.tedu.tmall.admin.mall.pojo.vo.GoodsVO;
import cn.tedu.tmall.admin.mall.service.IMallGoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
@Transactional
public class MallGoodsService implements IMallGoodsService {

    @Autowired
    private IGoodsRepository goodsRepository;

    @Autowired
    private ICategoryRepository categoryRepository;


    @Override
    public void addGoods(MallGoodsAddParam mallGoodsAddParam) {
        //1 判断文章标题是否重复
        String title = mallGoodsAddParam.getTitle();
        Integer goodsCount = goodsRepository.countGoodsByTitle(title);
        if (goodsCount == 0) {
            //不重复
            Long categoryId = mallGoodsAddParam.getCategoryId();
            CategoryPO categoryPO = categoryRepository.getCategoryById(categoryId);
            if (categoryPO == null) {
                throw new RuntimeException("类别不存在！");
            } else {
                Integer enable = categoryPO.getEnable();
                if (enable == 1) {
                    GoodsPO goodsPO = new GoodsPO();
                    BeanUtils.copyProperties(mallGoodsAddParam, goodsPO);
                    Integer goodsRows = goodsRepository.saveGoods(goodsPO);
                    if (goodsRows != 1) {
                        throw new RuntimeException("商品添加失败！");
                    }
                    GoodsDetailPO goodsDetailPO = new GoodsDetailPO();
                    goodsDetailPO.setGoodsId(goodsPO.getId());
                    goodsDetailPO.setDetail(mallGoodsAddParam.getName());
                    Integer goodsDetailRows = goodsRepository.saveGoodsDetail(goodsDetailPO);
                    if (goodsDetailRows != 1) {
                        throw new RuntimeException("商品添加失败！");
                    }
                } else {
                    throw new RuntimeException("该类别已下架，无法添加商品！");
                }
            }
        } else {
            throw new RuntimeException("添加失败,该商品已存在！");
        }
    }

    @Override
    public GoodsVO detail(Long goodsId) {
        GoodsPO goodsPO = goodsRepository.basic(goodsId);
        if (goodsPO == null) {
            throw new RuntimeException("商品不存在！");
        }
        GoodsDetailPO goodsDetailPO = goodsRepository.detail(goodsId);
        if (goodsDetailPO == null) {
            throw new RuntimeException("商品不存在！");
        }
        GoodsVO goodsVO = new GoodsVO();
        BeanUtils.copyProperties(goodsDetailPO, goodsVO);
        BeanUtils.copyProperties(goodsPO, goodsVO);
        return goodsVO;
    }

    @Override
    public List<GoodsItemListVO> listGoods(Long categoryId, Integer pageNum, Integer pageSize) {
        List<GoodsPO> goodsPOS = goodsRepository.listGoods(categoryId, pageNum, pageSize);
        return goodsItemListPO2listVO(goodsPOS);
    }

    private GoodsItemListVO goodsPO2VO(GoodsPO po){
        GoodsItemListVO vo = new GoodsItemListVO();
        BeanUtils.copyProperties(po,vo );
        return vo;
    }

    private List<GoodsItemListVO> goodsItemListPO2listVO(List<GoodsPO> pos){
        List<GoodsItemListVO> vos = new ArrayList<>();
        for (GoodsPO po : pos){
            vos.add(goodsPO2VO(po));
        }
        return vos;
    }
}
